/**
 * 
 */
package com.and.xmppsdk.fragment;

import org.drl.xmpp.Contact;
import org.drl.xmpp.RosterManager;
import org.drl.xmpp.TalkClient;

import com.and.xmppsdk.HomeActivity;
import com.and.xmppsdk.R;
import com.and.xmppsdk.XmppApplication;
import com.and.xmppsdk.adapter.ContactAdapter;
import com.and.xmppsdk.adapter.ContactGroupAdapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ListView;
import android.widget.TextView;

/**
 * @author hljdrl@gmail.com 联系人好友列表
 * 
 */
public final class ContactsFragment extends Fragment implements
		OnChildClickListener, OnItemClickListener {

	private static ContactAdapter adapter;
	private RosterManager roster;
	private TalkClient mTalkClient;
	private ListView mLV;
	private ExpandableListView mELV;
	private ContactGroupAdapter mContactGroupAdapter;
	private View rootView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		rootView = getView();
		mTalkClient = XmppApplication.getXmppSDK(getActivity());
		roster = mTalkClient.getRosterManager();
		if (adapter == null) {
			adapter = new ContactAdapter(getActivity(), roster.getContactList());
		}
		mLV = (ListView) rootView.findViewById(R.id.lst_contacts_1);
		mELV = (ExpandableListView) rootView.findViewById(R.id.lst_contacts_2);
		mELV.setOnChildClickListener(this);
		initAdapter1();
		mLV.setOnItemClickListener(this);

	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		menu.add(0, Menu.FIRST, Menu.FIRST, "无分组");
		menu.add(0, Menu.FIRST + 1, Menu.FIRST, "分组");
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case Menu.FIRST:
			initAdapter1();
			break;
		case Menu.FIRST + 1:
			initAdapter2();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void initAdapter1() {
		if (adapter == null) {
			adapter = new ContactAdapter(getActivity(), roster.getContactList());
		}
		if (mLV.getAdapter() != adapter) {
			mLV.setAdapter(adapter);
		}
		mELV.setVisibility(View.GONE);
		mLV.setVisibility(View.VISIBLE);
	}

	private void initAdapter2() {
		if (mContactGroupAdapter == null) {
			mContactGroupAdapter = new ContactGroupAdapter(getActivity(),
					roster);
		}
		if (mELV.getAdapter() == null
				|| mELV.getAdapter() != mContactGroupAdapter) {
			mELV.setAdapter(mContactGroupAdapter);
		}
		mLV.setVisibility(View.GONE);
		mELV.setVisibility(View.VISIBLE);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.root_ft_contacts, container,
				false);
		return view;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (mLV == arg0) {
			Contact mCm = adapter.getItem(arg2);
			Intent mIm = new Intent(getActivity(),
					com.and.xmppsdk.ChatActivity.class);
			mIm.putExtra("contact", mCm);
			startActivity(mIm);
		}
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		Contact mCm = mContactGroupAdapter.getChild(groupPosition,
				childPosition);
		if (mCm != null) {
			Intent mIm = new Intent(getActivity(),
					com.and.xmppsdk.ChatActivity.class);
			mIm.putExtra("contact", mCm);
			startActivity(mIm);
			return true;
		} else {
			return false;
		}

	}

}
